﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NewsPub;
using NewLife.CommonEntity;
using NewLife.Web;

public partial class AddCommunicate : MyPageBase<Communicate>
{
    //protected override void SaveFormSuccess()
    //{ NewLife.Log.XTrace.WriteLine("执行重写的savesuccess"); }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        //EntityForm.OnSetForm += EntityForm_OnSetForm;
        EntityForm.OnGetForm += EntityForm_OnGetForm;
        EntityForm.OnSaving+=EntityForm_OnSaving;
        EntityForm.OnSaveSuccess += EntityForm_OnSaveSuccess;
        EntityForm.OnSaveFailure += EntityForm_OnSaveFailure;
        EntityForm.OnGetEntity+=EntityForm_OnGetEntity;
        //EntityForm.OnSetForm += new EventHandler<EventArgs<IEntity>>(EntityForm_OnSetForm);
        //EntityForm.OnGetForm += new EventHandler<EventArgs<IEntity>>(EntityForm_OnGetForm);
        //EntityForm.OnSaveSuccess += new EventHandler<EventArgs<IEntity>>(EntityForm_OnSaveSuccess);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            //EntityForm.OnSaveSuccess += EntityForm_OnSaveSuccess;
            //EntityForm.OnSaveFailure+=EntityForm_OnSaveFailure;
            //EntityForm.OnSetForm+=EntityForm_OnSetForm;
            //EntityForm.OnGetForm += EntityForm_OnGetForm;
            //EntityForm.OnGetEntity+=EntityForm_OnGetEntity;
            foreach (Occupation o in Occupation.FindAllByParentID(0))
            {
                ListItem l = new ListItem();
                l.Value = o.ID.ToString();
                l.Text = o.TreeNodeName2;
                if (o.Childs.Count != 0) l.Attributes.Add("disabled", "disabled");// l.Enabled = false;
                frmOccupation.Items.Add(l);
            }
        }
    }
    void EntityForm_OnSaving(object sender, EntityFormEventArgs e)
    {
        Entity.CreateTime = DateTime.Now;
        NewLife.Log.XTrace.WriteLine("执行onsaving");
        NewLife.Log.XTrace.WriteLine("ID="+Entity.ID);
        string mailcontent= @"<p>
    {username}:
</p>
<p>
    &nbsp; &nbsp; 你好！
</p>
<p>
    &nbsp; &nbsp; 欢迎你来{Company}咨询，点击<a href='{siteurl}communicateDisp.aspx?ReferenceNumber={rn}' target='_blank' title='这里'><span style='background-color: rgb(255, 255, 255); color: rgb(255, 0, 0); '>这里</span></a>可以查询你的咨询信息以及回复情况。
</p>
<p>
    &nbsp; &nbsp; 如果需要更进一步了解，请致电{telephone}。
</p>
<p>
    <br/>
</p>";
        mailcontent = mailcontent.Replace("{username}", Entity.Name);
        mailcontent = mailcontent.Replace("{rn}", Entity.ReferenceNumber);
        mailcontent = mailcontent.Replace("{siteurl}", XTLCMS.SiteConfig.Current.Url);
        mailcontent = mailcontent.Replace("{telephone}", XTLCMS.SiteConfig.Current.Tel);
        mailcontent = mailcontent.Replace("{Company}", XTLCMS.SiteConfig.Current.Company);
        XTLCMS.MailConfig.sendMail(Entity.Email, "你的咨询信息", mailcontent);

    }
    //void OnSaveFailure(object sender, EntityFormEventArgs e)
    //{
    //    throw new NotImplementedException();
    //}
     void EntityForm_OnGetEntity(object sender, EntityFormEventArgs e) {
         this.Entity.ReferenceNumber = "YXEPB-" + Guid.NewGuid().ToString(); 
         //frmReferenceNumber.Text = "YXEPB-" + Guid.NewGuid().ToString();
         
     }
     
    void EntityForm_OnGetForm(object sender, EntityFormEventArgs e) { 
        //this.Entity.ReferenceNumber = "YXEPB-" + Guid.NewGuid().ToString();
    string code = frmVCode.Text.Trim();
    if (Session["dt_session_code"] == null)
    {
        frmVCode.Text = null;
        WebHelper.AlertAndEnd("验证码输入不正确！");
        return;
    }
    if (code.ToLower() != Session["dt_session_code"].ToString().ToLower())
    {
        //lblTip.Visible = true;
        //lblTip.Text = "验证码输入不正确";
        WebHelper.AlertAndEnd("验证码输入不正确！");
        frmVCode.Text = null;
        return;
    }
    }
    void EntityForm_OnSetForm(object sender, EntityFormEventArgs e) {  }
    void EntityForm_OnSaveSuccess(object sender, EntityFormEventArgs e)
    {
        NewLife.Log.XTrace.WriteLine("执行onsavesuccess");
        //e.Cancel = true;
        //Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", @"alert('提交诉求成功！');
//", true);
        //WebHelper.AlertAndRedirect("提交成功！", "Default.aspx");
    }
    void EntityForm_OnSaveFailure(object sender, EntityFormEventArgs e)
    {
        //WebHelper.AlertAndEnd("输入不正确！");
        NewLife.Log.XTrace.WriteLine("执行onsavefailure");
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        
    }


    protected void CompareValidator1_Load(object sender, EventArgs e)
    {
        
    }
    //protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    //{
    //    string code = frmVCode.Text.Trim();
    //    NewLife.Log.XTrace.WriteLine("验证码为："+code);
    //    NewLife.Log.XTrace.WriteLine("Session验证码为：" + Session["dt_session_code"].ToString().ToLower());
    //    if (Session["dt_session_code"] == null)
    //    {
    //        CustomValidator1.IsValid = false;
    //        NewLife.Log.XTrace.WriteLine("session验证码为空" );
    //        return;
    //    }
    //    CustomValidator1.IsValid = code.ToLower() == Session["dt_session_code"].ToString().ToLower();
    //}
    //protected void frmVCode_TextChanged(object sender, EventArgs e)
    //{
    //    string code = frmVCode.Text.Trim();
    //    NewLife.Log.XTrace.WriteLine("验证码为：" + code);
    //    NewLife.Log.XTrace.WriteLine("Session验证码为：" + Session["dt_session_code"].ToString().ToLower());
    //    if (Session["dt_session_code"] == null)
    //    {
    //        CustomValidator1.IsValid = false;
    //        NewLife.Log.XTrace.WriteLine("session验证码为空");
    //        return;
    //    }
    //    CustomValidator1.IsValid = code.ToLower() == Session["dt_session_code"].ToString().ToLower();
    //}
}